﻿using System;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dack_Web2.DTO;
using Dack_Web2.BUS;
using System.Data;

namespace Dack_Web2
{
    public partial class ThongTinSanPham : System.Web.UI.Page
    {
        public string Datestring;
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Request.QueryString["MaSanPham"] == null) Response.Redirect("~/Default.aspx");

            if (!IsPostBack)
            {
                string MaSanPham = Request.QueryString["MaSanPham"];
                string sql = "select * from SanPham P, LoaiSanPham C , Deal D where D.MaSanPham=P.MaSanPham and P.MaLoaiSanPham=C.MaLoaiSanPham and P.MaSanPham=" + MaSanPham;
                DataTable dtSanPham = Sql_DataProvider.SQL_ExecuteTable(sql);
                DataRow DongSP = dtSanPham.Rows[0];
                string format = "ddd MMM d yyyy HH:mm:ss";
                DateTime _TempDate = (DateTime)DongSP["NgayKetThuc"];
                Datestring = _TempDate.ToString(format);
                if (DateTime.Now.Date >= (DateTime)DongSP["NgayKetThuc"])
                {
                    btMuaHang.Enabled = false;
                }
                User1();
                imgHinhGioiThieu.ImageUrl = "~\\Images\\SanPham\\" + DongSP["HinhGioiThieu"].ToString();
                lbTenSanPham.Text = DongSP["TenSanPham"].ToString();
                lbGiaSanPham.Text = string.Format("{0:#,#.##}", DongSP["GiaSanPham"]);
                lbThongTinChiTiet.Text = DongSP["ThongTinChiTiet"].ToString();
                lbGiaGiam.Text = string.Format("{0:#,#.##}", GioHang.GiaSanPham(int.Parse(DongSP["MaDeal"].ToString())));
                lbTietKiem.Text = DongSP["Giam"].ToString();
                btMuaHang.PostBackUrl = "~/DatHang.aspx?MaDeal=" + DongSP["MaDeal"].ToString();
            }

            
            if (!CurrentContext.IsLogged)
            {
                Panel1.Visible = true;
                Panel1.Enabled = true;
                

            }
            else
            {
                Panel2.Enabled = true;
                Panel2.Visible = true;
                Panel3.Enabled = true;
                Panel3.Visible = true;
                if (CurrentContext.UserName == "Admin")
                {
                    dlCommand.Enabled = true;
                }
            }
            
        }
            
       void User1()
       {
           
           string sql = "select cm.NoiDung, cm.ThoiGian, us.TenDangNhap  from [Deal] d, [User] us, [Comment] cm where d.MaDeal = cm.MaDeal and cm.MaUser = us.MaUser and d.MaSanPham = " + Request.QueryString["MaSanPham"];
           DataTable dtUser = Sql_DataProvider.SQL_ExecuteTable(sql);
           dlCommand.DataSource = dtUser;
           dlCommand.DataBind();
       }
              
       protected void btGui_Click(object sender, EventArgs e)
       {
           CommentDTO cm = new CommentDTO();
           CommentBUS bus = new CommentBUS();

           string tendangnhap = CurrentContext.UserName;
           String sql = "select MaUser from [User] where TenDangNhap = '" + tendangnhap + "'";
           cm.MaUser = int.Parse(Sql_DataProvider.SQL_ExecuteScalar(sql).ToString());
           string MaSanPham = Request.QueryString["MaSanPham"].ToString();
           string sql1 = "select MaDeal from [Deal] where MaSanPham= " + MaSanPham;
           cm.MaDeal = int.Parse(Sql_DataProvider.SQL_ExecuteScalar(sql1).ToString());
           cm.NoiDung = txtNoiDung.Text;
           cm.ThoiGian = DateTime.Now.Date;
           bus.Insert(cm);
           User1();
       }


       
    }
}

               

        

    
